<?php


namespace app\controller\admin\system\merchant;


use app\common\repositories\store\product\ProductCopyRepository;
use app\common\repositories\system\merchant\MerchantCategoryIntentionRepository;
use crmeb\basic\BaseController;
use think\App;
use think\db\exception\DbException;


class CategoryIntention extends BaseController
{
    /**
     * @var MerchantCategoryIntentionRepository
     */
    protected $repository;

    /**
     * CategoryIntention constructor.
     * @param App $app
     * @param MerchantCategoryIntentionRepository $repository
     */
    public function __construct(App $app, MerchantCategoryIntentionRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }
    /**
     * @return mixed
     * @author: fupeng
     * @day: 2023/10/17
     */
    public function lst()
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['keyword', 'date', 'status']);
        return app('json')->success($this->repository->lst(null, $where, $page, $limit));
    }

    /**
     * @return mixed
     * @author: fupeng
     * @day: 2023/10/17
     */
    public function getStatusFilter()
    {
        return app('json')->success($this->repository->getFilter(0));
    }

    public function statusForm($id)
    {
        if (!$this->repository->getWhereCount(['mer_category_intention_id' => $id, 'is_del' => 0]))
            return app('json')->fail('数据不存在');
        return app('json')->success(formToData($this->repository->statusForm($id)));
    }


    public function switchStatus($id)
    {
        if (!$this->repository->getWhereCount(['mer_category_intention_id' => $id, 'is_del' => 0]))
            return app('json')->fail('数据不存在');
        $data = $this->request->params(['status', 'fail_msg']);
        $data['status'] = $data['status'] == 1 ? 1 : 2;
        $this->repository->updateStatus($id, $data);
        return app('json')->success('修改成功');
    }

    public function form($id)
    {
        if (!$this->repository->getWhereCount(['mer_category_intention_id' => $id, 'is_del' => 0]))
            return app('json')->fail('数据不存在');
        return app('json')->success(formToData($this->repository->markForm($id)));
    }
    public function mark($id)
    {
        if (!$this->repository->getWhereCount(['mer_category_intention_id' => $id, 'is_del' => 0]))
            return app('json')->fail('数据不存在');
        $data = $this->request->param('mark');
        $this->repository->update($id, ['mark' => $data]);
        return app('json')->success('修改成功');
    }


    /**
     * @param $id
     * @return mixed
     * @throws DbException
     * @author: fupeng
     * @day: 2023/10/17
     */
    public function delete($id)
    {
        if (!$this->repository->getWhereCount(['mer_category_intention_id' => $id, 'is_del' => 0]))
            return app('json')->fail('数据不存在');
        $this->repository->update($id, ['is_del' => 1]);
        return app('json')->success('删除成功');
    }
}
